/**    
 * @Description: 需要编辑正文的流程，统一用正文上传保存Action
 * @author shuq techisone@gmail.com  
 * @date 2012-4-26 上午09:56:35 
 * @version V1.0   
 */

package net.sxnic.workflow.zw.action;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;

import net.sxnic.comm.utils.PropertyUtil;
import net.sxnic.ugr.context.AppContext;
import net.sxnic.workflow.WfActionSupport;
import net.sxnic.workflow.task.Task;
import net.sxnic.workflow.utils.WorkflowUtils;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

public class UploadDoc extends WfActionSupport {

	private static final long serialVersionUID = 1L;

	private String zwtype;

	private File AipFile;

	private InputStream inputStream;

	public String execute() throws Exception {

		String docType = PropertyUtil.getProperty("senddoc.operate.type");

		wfentity = wfenManager.get(id);

		if (StringUtils.isBlank(zwtype)) {
			zwtype = wfentity.getCate2();
		}

		String filePath = WorkflowUtils.getSendDocPath(id, wfentity.getCate1(),
				wfentity.getNigaoYear()) + "-" + zwtype + "." + docType;
		File oldFile = new File(filePath);
		// 每一次调整之后都有一个备份
		if (oldFile.exists()) {
			// 判断md5 是否修改过
			String oldMd5 = WorkflowUtils.md5File(oldFile);
			String newMd5 = WorkflowUtils.md5File(AipFile);

			if(!oldMd5.equals(newMd5)){
				Task t = taskManager.get(taskId);
				String wrapperFilePath = WorkflowUtils.getSendDocPath(id,
						wfentity.getCate1(), wfentity.getNigaoYear())
						+ File.separator +AppContext.getUserName(request)+ t.getName()+"." + docType;
				FileUtils.copyFile(AipFile, new File(wrapperFilePath));
			}
		}

		FileUtils.copyFile(AipFile, oldFile);

		String returnVaule = "保存成功";
		inputStream = new ByteArrayInputStream(returnVaule.getBytes("UTF-8"));
		return SUCCESS;
	}

	public File getAipFile() {
		return AipFile;
	}

	public void setAipFile(File aipFile) {
		AipFile = aipFile;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getZwtype() {
		return zwtype;
	}

	public void setZwtype(String zwtype) {
		this.zwtype = zwtype;
	}

}
